package com.mdd.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mdd.common.CustomException;
import com.mdd.common.R;
import com.mdd.pojo.Blog;
import com.mdd.pojo.request.BlogAddRequest;
import com.mdd.pojo.request.BlogQueryRequest;
import com.mdd.pojo.request.BlogReplyRequest;
import com.mdd.pojo.request.BlogUpdateRequest;
import com.mdd.pojo.vo.BlogVo;
import com.mdd.service.BlogService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
@RequestMapping("/blog")
public class BlogController {

    @Autowired
    private BlogService blogService;

    /**
     * 用户写评论
     * @param blogAddRequest
     * @return
     */
    @PostMapping("/add")
    public R<Long> addBlog(@RequestBody BlogAddRequest blogAddRequest){
        //校验
        if (blogAddRequest == null) {
            throw new CustomException("请求参数有误");
        }
        Long blogId = blogService.addBlog(blogAddRequest);
        return R.success(blogId);
    }

    /**
     * 用户删除评论
     * @param id
     * @return
     */
    @PostMapping("/delete")
    public R<Boolean> removeBlog(Long id){
        //校验
        if (id == null) {
            R.error("请求参数有误");
        }
        boolean res = blogService.removeBlog(id);
        return R.success(res);
    }

    /**
     * 修改评论
     * @param blogUpdateRequest
     * @return
     */
    @PostMapping("/update")
    public R<Boolean> updateBlog(@RequestBody BlogUpdateRequest blogUpdateRequest){
        //校验
        if (blogUpdateRequest == null) {
            throw new CustomException("请求参数为空");
        }
        boolean res = blogService.updateBlog(blogUpdateRequest);
        return R.success(res);
    }

    /**
     * 评论列表/分页
     * @return
     */
    @GetMapping("/list")
    public R<Page<BlogVo>> blogList(BlogQueryRequest blogQueryRequest){
        //检验
        if (blogQueryRequest == null) {
            throw new CustomException("请求参数为空");
        }
        Page<BlogVo> blogVoPage = blogService.blogList(blogQueryRequest);
        return R.success(blogVoPage) ;
    }

    /**
     * 根据id获取评论
     * @param id
     * @return
     */
    @GetMapping("/get")
    public R<BlogVo> blogById(Long id){
        if (id == null || id <= 0) {
            throw new CustomException("请求参数有误");
        }
        Blog byId = blogService.getById(id);
        if (byId == null) {
            throw new CustomException("没有该评论");
        }
        BlogVo blogVo = new BlogVo();
        BeanUtils.copyProperties(byId,blogVo);
        return R.success(blogVo);
    }

    /**
     * 用户点赞评论
     * @param id
     * @return
     */
    @PostMapping("/like/{id}")
    public R<Boolean> likeBlog(@PathVariable("id") Long id){
        //校验
        if (id == null) {
            throw new CustomException("请求参数为空");
        }
        boolean res = blogService.userLikeBlog(id);
        return R.success(res);
    }

    @PostMapping("/reply")
    public R<Boolean> replyBlog(@RequestBody BlogReplyRequest blogReplyRequest){
        //校验
        if (blogReplyRequest == null) {
            throw new CustomException("请求参数为空");
        }
        boolean res = blogService.replyBlog(blogReplyRequest);
        return R.success(res);
    }

}
